package freemarker;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;


/**
 * Freemarker工具
 *
 * @author yanzhao
 * @date 2021/12/6 23:15
 * @since 1.0.0
 */
public class FreemarkerUtil {

    /**
     * classpath
     */
    private static Configuration classPathConfiguration;

    public static final String TEMPLATE_DIR = "/tmp/framework/starter/templates";

    /**
     * 指定目录
     */
    private static Configuration configuration;

    public static Configuration getClassPathConfiguration() throws IOException {
        if (classPathConfiguration != null) {
            return classPathConfiguration;
        }

        synchronized (FreemarkerUtil.class) {
            if (classPathConfiguration != null) {
                return classPathConfiguration;
            }

            classPathConfiguration = new Configuration(Configuration.VERSION_2_3_30);
            classPathConfiguration.setDefaultEncoding("utf-8");

            //加载模板文件(模板的路径)  这个操作在打包成jar之后会报错
//            File file = ResourceUtils.getFile("classpath:freemarker");
//            configuration.setDirectoryForTemplateLoading(file);
            classPathConfiguration.setClassForTemplateLoading(FreemarkerUtil.class, "/");

            return classPathConfiguration;
        }
    }

    public static Configuration getConfiguration() throws IOException {
        if (configuration != null) {
            return configuration;
        }

        synchronized (FreemarkerUtil.class) {
            if (configuration != null) {
                return configuration;
            }

            configuration = new Configuration(Configuration.VERSION_2_3_30);
            configuration.setDefaultEncoding("utf-8");
            configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_DIR));
            return configuration;
        }
    }

    public static void writeFromClasspath(String ftlName, File targetFile, Object param) throws IOException, TemplateException {
        Configuration configuration = FreemarkerUtil.getClassPathConfiguration();
        write(ftlName, targetFile, param, configuration);
    }

    public static void writeFromDynamic(String ftlName, File targetFile, Object param) throws IOException, TemplateException {
        Configuration configuration = FreemarkerUtil.getConfiguration();
        write(ftlName, targetFile, param, configuration);
    }

    public static void write(String ftlName, File targetFile, Object param, Configuration configuration) throws IOException, TemplateException {
        Template template = configuration.getTemplate(ftlName);
        Writer out = new FileWriter(targetFile);
        template.process(param, out);
        out.flush();
        out.close();
    }

    public static String writeMemory(String ftlName, Object param, Configuration configuration) throws IOException, TemplateException {
        try (StringWriter writer = new StringWriter()) {
            Template template = configuration.getTemplate(ftlName);
            template.process(param, writer);
            writer.flush();
            return writer.toString();
        }
    }

    public static void main(String[] args) throws IOException, TemplateException {
        final Configuration classPathConfiguration = getClassPathConfiguration();
        final HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("abc", "变量abc的值");
        System.out.println(writeMemory("/ftl/test.ftl", objectObjectHashMap, classPathConfiguration));
    }

}
